<?php
class CategoriesController extends Controller
{
	public $layout = 'main';
	public function actionIndex()
	{		
		$arrCategories = Categories::getInstance()->getByParent();
		/*for ordering (swap position)*/
		foreach($arrCategories as $key => $item){
			//down
			for($i=$key; $i<count($arrCategories); $i++){
				if(isset($arrCategories[$i+1]) && $item['category_parent'] == $arrCategories[$i+1]['category_parent']){
					$arrCategories[$key]['category_id_next'] = $arrCategories[$i+1]['category_id'];
					break;
				}
				else{
					$arrCategories[$key]['category_id_next'] = null;
				}
			}
			//up
			for($i=$key; $i>=0; $i--){
				if(isset($arrCategories[$i-1]) && $item['category_parent'] == $arrCategories[$i-1]['category_parent']){
					$arrCategories[$key]['category_id_prev'] = $arrCategories[$i-1]['category_id'];
					break;
				}
				else{
					$arrCategories[$key]['category_id_prev'] = null;
				}
			}
		}
		$this->render('index-order', array(
			'arrCategories' => $arrCategories
		));
	}
	
	public function actionForm()
	{
		$intPostId = Yii::app()->request->getParam('id', null);
		if(Yii::app()->request->isPostRequest)
		{
			$arrParams = Yii::app()->request->getParam('categoryForm', null); 
			if(empty($intPostId))
				$result = Categories::getInstance()->insert($arrParams);
			else
				$result = Categories::getInstance()->update($intPostId, $arrParams);
		}
		$arrItemInfo = array();
		if(!empty($intPostId)){
			$arrItemInfo = Categories::getInstance()->get($intPostId);
		}
		$arrCategories = Categories::getInstance()->getByParent();
		$this->render('form', array(
			'arrItemInfo' => $arrItemInfo,
			'arrCategories' => $arrCategories
		));
	}
	
	public function actionDelete(){
		$intId = Yii::app()->request->getParam('id', null);
		if(!empty($intId))
				$result = Categories::getInstance()->delete($intId);
		header('Location: '.LINK_ADMINCP_CATEGORIES);
	}

	public function actionSwap(){
		$intId1 = Yii::app()->request->getParam('id1', null);
		$intId2 = Yii::app()->request->getParam('id2', null);
		if(Yii::app()->request->isAjaxRequest && Yii::app()->request->isPostRequest){
				$intResult = Categories::getInstance()->swap($intId1, $intId2);
				$strMessage = '';
				echo json_encode(array('result'=>$intResult, 'msg'=>$strMessage));
		}
		die;
	}
	
	public function actionUpdate_Hierarchy(){
		$arrParams = Yii::app()->request->getParam('ul', null); 
		if(Yii::app()->request->isAjaxRequest && Yii::app()->request->isPostRequest){
			$args = $this->buildHierarchy($arrParams);;
			Categories::getInstance()->updateHierarchy($args);
			if($arrParams) {
				print_r($args);
				print_r($arrParams); die;
			}
		}
	}
	
	private function buildHierarchy(array $arrParams, &$tree = array()) {
		foreach($arrParams as $R){
			if(!empty($R['children'])){
				foreach($R['children'] as $child)
					$tree[$R['id']][] = $child['id'];
					$this->buildHierarchy($R['children'], $tree);
			}else {
				$tree[$R['id']] = array();
			}
		}
		return $tree;
	}
}